package com.dxy.lc.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dxy.lc.model.entity.Memo;
import com.dxy.lc.mapper.MemoMapper;
import com.dxy.lc.service.MemoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxy.lc.utils.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
* <p>
*  服务实现类
* </p>
*
* @author dxy
* @since 2024-12-03
*/
@Slf4j
@Service
public class MemoServiceImpl extends ServiceImpl<MemoMapper, Memo> implements MemoService {

    @Override
    public void add(Memo entity) {
        Integer loginUserId = Integer.parseInt(LoginUtil.getLoginUserIdOrException());
        Memo m = new Memo();
        m.setUserId(loginUserId);
        m.setContent(entity.getContent());
        saveOrUpdate(m, new LambdaUpdateWrapper<Memo>().eq(Memo::getUserId, loginUserId));
    }

    @Override
    public void updById(Memo entity) {
        Assert.notNull(entity.getId());
        updateById(entity);
    }

    @Override
    public Memo getUserMemo() {
        Integer loginUserId = Integer.parseInt(LoginUtil.getLoginUserIdOrException());
        return getOne(new LambdaQueryWrapper<Memo>().eq(Memo::getUserId, loginUserId));
    }


}
